package com.sea.model.repository;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import com.sea.model.Artefacto;
import com.sea.model.repository.ArtefactosRepository;


public class ArtefactosRepository implements Entity<Artefacto>{
	
	
	private EntityManager em;	
	
	public ArtefactosRepository(EntityManager em) {
		this.em = em;
	}

	@Override
	public Artefacto insert(Artefacto t) {
		// TODO Auto-generated method stub
		em.persist(t);
		return t;
	}

	@Override
	public void update(Artefacto t) {
		EntityTransaction tx=em.getTransaction();
			try{
						
					tx.begin();
					em.merge(t);
					tx.commit();
			}catch (Exception e) {
				e.printStackTrace();
				tx.rollback();
				throw new  RuntimeException("No se pudo Actualizar");
			}
	}

	@Override
	public void delete(Artefacto t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Artefacto findById(String id) {
		// TODO Auto-generated method stub
		return em.find(Artefacto.class, Integer.parseInt(id));
	}

	@Override
	public Artefacto findById(Artefacto t) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public List<Artefacto> listAll() {
		// TODO Auto-generated method stub
		Query query=em.createQuery("Select o From Artefacto o where o.estado='PENDIENTE'");
		List<Artefacto> artefactos=query.getResultList();
		return artefactos;
	}

	@Override
	public List<Artefacto> listByCustomCriteria(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Artefacto findById(int id) {
		// TODO Auto-generated method stub
		return em.find(Artefacto.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Artefacto> listDesp() {
		// TODO Auto-generated method stub
		Query query=em.createQuery("Select o From Artefactos o");
		List<Artefacto> entregado=query.getResultList();
		return entregado;
	}
	
	
}
